﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

using WMS.Common;
using WMS.InventoryManagement.Differences.DTO;
using WMS.Pagination;

namespace WMS.InventoryManagement.Differences
{
    public class DifferencesAppService:ApplicationService, IDifferencesAppService
    {
        private readonly IRepository<Difference,int> _differenceRepository;

        public DifferencesAppService(IRepository<Difference, int> differenceRepository)
        {
            _differenceRepository = differenceRepository;
        }

        public async Task<PageResult> GetListAsync(string? stocker, int page, int limit)
        {
            var differencelist = await _differenceRepository.GetListAsync();

            differencelist = differencelist.WhereIf(!string.IsNullOrEmpty(stocker), x => x.Stocker.Contains(stocker)).ToList();
            PageResult pageResult = new PageResult();
            pageResult.RowCount = differencelist.Count();
            pageResult.PageCount = (int)Math.Ceiling(pageResult.RowCount * 1.0 / limit);
            pageResult.Queryable = differencelist.Skip((page - 1) * limit).Take(limit).AsQueryable();
            return pageResult;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="vendor">供应商信息</param>
        /// <returns>返回受影响数</returns>
        public int CreateAsync(Difference difference)
        {
            var repetition =  _differenceRepository.GetListAsync(m => m.Stocker == difference.Stocker).Result;
            if (repetition.Count() > 0)
            {
                return -1;
            }
             _differenceRepository.InsertAsync(difference);
            return 1;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="differenceId">主键Id</param>
        /// <returns>返回受影响行数</returns>
        public int DeleteAsync(int differenceId)
        {
             _differenceRepository.DeleteAsync(differenceId);
            return 1;
        }

        /// <summary>
        /// 获取对应数据
        /// </summary>
        /// <param name="differenceId">主键Id</param>
        /// <returns>返回受影响行数</returns>
        public Difference GetDifferenceById(int id)
        {
            var vendorList = _differenceRepository.FindAsync(id).Result;
            return vendorList;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        public async Task<int> UpdateAsync(UpdateDifferentDTO input)
        {
            var list = await _differenceRepository.FindAsync(v =>v.Id == input.Id);
            if (list==null)
            {
                return -1;
            }
            list.Stocker = input.Stocker;
            list.StockDate = input.StockDate;
            list.Storage = input.Storage;
            list.Tray = input.Tray;
            list.CommodityCode = input.CommodityCode;
            list.CommodityName = input.CommodityName;
            list.Accounts = input.Accounts;
            list.ProtructDate = input.ProtructDate;
            list.StockNum = input.StockNum;
            list.Owner = input.Owner;
            list.OwnerCode = input.OwnerCode;
            list.StockStatus = input.StockStatus;


            await _differenceRepository.UpdateAsync(list);
            return 1;
        }


    }
}
